/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2014 All Rights Reserved.
 */
package com.h2o3.carmack.message.json;

import com.alibaba.fastjson.JSONObject;
import com.h2o3.carmack.message.Message;
import com.h2o3.carmack.message.MessageSerializer;

/**
 * @author chensheng
 * @version $Id: JsonMessageSerializer.java, v 0.1 1/22/14 10:33 PM chensheng Exp $
 */
public class JsonMessageSerializer implements MessageSerializer {
    @Override
    public String writeMessage(Message message) {
        JsonMessage jsonMessage = (JsonMessage) message;

        return jsonMessage.getJsonObject().toJSONString();
    }

    @Override
    public Message readMessage(String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        return new JsonMessage(jsonObject);
    }
}
